package com.wangyadong.hobby.javanetwork.address;

import java.io.IOException;
import java.net.*;
import java.util.Arrays;
import java.util.Enumeration;

/**
 * <p>
 *
 * @author <714037465@qq.com>
 * @since 2019/7/99:12
 **/
public class InetAddressTest {

    public static void main(String[] args) throws IOException {
//        InetAddress address = InetAddress.getByName("weixinaws.cnecloud.com");
//        System.out.println("000000000000000000000000000000000000");
//        System.out.println(address.getHostName());
//        byte[] address1 = address.getAddress();
//        System.out.println("================================");
//        Arrays.stream(new byte[][]{address1}).forEach(System.out::println);
//        System.out.println(address);
//        InetAddress byName = InetAddress.getByName("52.83.227.65");
//        System.out.println(byName.getHostName());
//        String canonicalHostName = byName.getCanonicalHostName();
//        System.out.println(address.getCanonicalHostName() + "===========================" + canonicalHostName);
        InetAddress[] allByName = InetAddress.getAllByName("www.baidu.com");
        Arrays.stream(allByName).forEach(System.out::println);
//        InetAddress[] allByName1 = InetAddress.getAllByName("52.83.227.65");
//        Arrays.stream(allByName1).forEach(System.out::println);
//        System.out.println(InetAddress.getLocalHost());//本机IP地址
//        System.out.println("--------------------------------------------------------------");
//        byte[] addressByIp = {10, 10, (byte) 33, (byte) 228};
//        InetAddress byAddress = InetAddress.getByAddress(addressByIp);
//        System.out.println("Ip地址" + byAddress);
        /***
         *更改解析DNS成功的地址缓存时间
         * networkaddress.cache.ttl
         * 更改解析DNS不成功的地址缓存时间
         * networkaddress.cache.negative.ttl
         * -1 永不过期
         *
         * DNS地址可能会更改
         * 所以在此期间可能会报很多错误 UnKnowHostExeception,ConnectExeception,NoRouteToHostException
         */
        //按Ip地址查找
//        security();
//        reverse();
//        myaddress();
//        getVersion();
//        ipTest();
//        isReachable();
//        equals();
//        netWorkIntgerface();
        spamCheck(args);
    }

    /**
     * 提供一个IP地址作为参数,会为所请求的IP地址创建一个InetAddress对象，而不检查DNS
     * 只有通过getHostName的时候才会检查DNS
     * 如果通过getHostName的时候 并确定了DNS查找,但是IP地址没有找到，则不会抛出UnKonwHostExwception
     * 优先使用主机名 ，然后才是ip
     */
    public static void getByName() {
    }

    /***
     *主要用来检查是否可以解析DNS地址
     */
    public static void security() {
        System.out.println("begin security");
        SecurityManager manager = new SecurityManager();
        manager.checkConnect("10.10.15.52", 3306);
        System.out.println("end security");
    }


    public static void reverse() throws UnknownHostException {
        System.out.println("begin reverse");
        InetAddress byName = InetAddress.getByName("10.10.15.51");
        System.out.println(byName.getCanonicalHostName());
        System.out.println("end reverse");
    }


    public static void myaddress() throws UnknownHostException {
        System.out.println("begin myaddress");
        InetAddress localHost = InetAddress.getLocalHost();
        System.out.println(localHost.getHostAddress());
        System.out.println("end myaddress");
    }

    public static void getVersion() throws UnknownHostException {
        InetAddress localHost = InetAddress.getLocalHost();
        byte[] address = localHost.getAddress();
        if (address.length == 4) {
            System.out.println("is IP V4");
        } else if (address.length == 16) {
            System.out.println("is IP V6");
        }
    }

    public static void ipTest() throws UnknownHostException {
        InetAddress localHost = InetAddress.getLocalHost();
        boolean anyLocalAddress = localHost.isAnyLocalAddress();//0.0.0.0或0:0:0:0:0:0:0:0
        boolean loopbackAddress = localHost.isLoopbackAddress();//127.0.01或0:0:0:0:0:0:0:1
        boolean linkLocalAddress = localHost.isLinkLocalAddress();//ip v6 返回true
        boolean siteLocalAddress = localHost.isSiteLocalAddress();//ip v6 返回true
        boolean multicastAddress = localHost.isMulticastAddress();//ip 组播地址，发送给所有的计算机
        boolean mcGlobal = localHost.isMCGlobal();//全球组播 组播地址以FF开头 IPV6以FF0E FF1E
        boolean mcOrgLocal = localHost.isMCOrgLocal();// 组织内广播
        boolean mcSiteLocal = localHost.isMCSiteLocal();//只在本地网站中传输
        boolean mcLinkLocal = localHost.isMCLinkLocal();//是不是只在子网内传输
        boolean mcNodeLocal = localHost.isMCNodeLocal();//不能发送到最初网络接口以外、
        System.out.println("anyLocalAddress=>" + anyLocalAddress + " loopbackAddress=>" + loopbackAddress + "  linkLocalAddress=>" + linkLocalAddress
                + "  siteLocalAddress=>" + siteLocalAddress + " multicastAddress=>" + multicastAddress + "  mcGlobal=>" + mcGlobal + "  mcOrgLocal=>" + mcOrgLocal
                + " mcSiteLocal=>" + mcSiteLocal + "  mcLinkLocal=>" + mcLinkLocal + "  mcNodeLocal=>" + mcNodeLocal);

    }


    public static void isReachable() throws IOException {
        InetAddress localHost = InetAddress.getLocalHost();
        boolean reachable = localHost.isReachable(10);
        System.out.println(" is reachable=>" + reachable);
    }

    public static void equals() throws UnknownHostException {
        InetAddress localHost = InetAddress.getLocalHost();
        InetAddress localhost = InetAddress.getByName("localhost");
        boolean equals = localHost.equals(localhost);
        System.out.println("是否相等--》" + equals);
    }


    /**
     * isIPv4CompatibleAddress 0:0:0:0:0:0:0:0:xxxxxx
     * 也就是说最后四节不是0，可以截取后来创建一个ipv4的地址
     *
     * @throws UnknownHostException
     */
    public static void ipV4() throws UnknownHostException {
        Inet6Address localHost = (Inet6Address) Inet6Address.getLocalHost();
        boolean iPv4CompatibleAddress = localHost.isIPv4CompatibleAddress();
    }

    public static void ipV6() {
    }

    /**
     * 表示本机的一个IP地址，可以是
     */
    public static void netWorkIntgerface() throws SocketException, UnknownHostException {
        InetAddress localHost = InetAddress.getLocalHost();
        NetworkInterface byName = NetworkInterface.getByName("wlan1");
        System.out.println(byName.getName() + " " + byName.getInetAddresses());
        NetworkInterface byInetAddress = NetworkInterface.getByInetAddress(localHost);
        System.out.println(byInetAddress);
        System.out.println("======================================================");
        Enumeration<NetworkInterface> networkInterfaces = NetworkInterface.getNetworkInterfaces();
        while (networkInterfaces.hasMoreElements()) {
            System.out.println(networkInterfaces.nextElement());
        }
        System.out.println("======================================================");
        Enumeration<InetAddress> inetAddresses = byName.getInetAddresses();
        while (inetAddresses.hasMoreElements()) {
            System.out.println(inetAddresses.nextElement());
        }
    }

    public static final String BLACKHOLE = "sbl.spamhaus.org";

    public static void spamCheck(String args[]) {
        for (String arg : args) {
            if (isSpammer(arg)) {
                System.out.println("arg +" + arg + " is a know spammer");
            } else {
                System.out.println("arg +" + arg + " appears legitimate");
            }
        }
    }

    public static boolean isSpammer(String arg) {
        try {
            InetAddress byName = InetAddress.getByName(arg);
            byte[] address = byName.getAddress();
            String query = BLACKHOLE;
            for (byte octet : address) {
                int unsignedByte = octet < 0 ? octet + 256 : octet;
                query = unsignedByte + "." + query;
            }
            InetAddress byName1 = InetAddress.getByName(query);
            return true;
        } catch (UnknownHostException e) {
            e.printStackTrace();
            return false;
        }
    }


}
